<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace app\admin\controller;

use api\mall\model\InteriorConfigModel;
use cmf\controller\AdminBaseController;

/**
 * Description of InteriorController
 *
 * @author PC
 */
class InteriorController extends AdminBaseController
{

    /**
     * 内景管理
     * @adminMenu(
     *     'name'   => '内景管理',
     *     'parent' => 'admin/Interior/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '内景管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $model = new InteriorConfigModel();
        $data = $model->order('big_screen_index asc')->order('picture_index asc')->select();
        $scScreenArray = [];
        $scPictureArray = [];
        $mrScreenArray = [];
        $mrPictureArray = [];
        $lsScreenArray = [];
        $lsPictureArray = [];
        foreach ($data as $key => $value) {
            if ($value['interior_scene'] == 1) {
                if ($value['interior_type'] == 1) {
                    array_push($scScreenArray, $value);
                } else {
                    array_push($scPictureArray, $value);
                }
            } else if ($value['interior_scene'] == 2) {
                if ($value['interior_type'] == 1) {
                    array_push($mrScreenArray, $value);
                } else {
                    array_push($mrPictureArray, $value);
                }
            } else if ($value['interior_scene'] == 3) {
                if ($value['interior_type'] == 1) {
                    array_push($lsScreenArray, $value);
                } else {
                    array_push($lsPictureArray, $value);
                }
            }
        }
        $this->assign('scScreenArray', $scScreenArray);
        $this->assign('scPictureArray', $scPictureArray);
        $this->assign('mrScreenArray', $mrScreenArray);
        $this->assign('mrPictureArray', $mrPictureArray);
        $this->assign('lsScreenArray', $lsScreenArray);
        $this->assign('lsPictureArray', $lsPictureArray);
        return $this->fetch('index');
    }

    /**
     * 添加内景提交
     * @adminMenu(
     *     'name'   => '添加内景提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加内景提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $model = new InteriorConfigModel();
        $model->delete();

        $data = $this->request->param();

        $scScreens = !empty($data['scScreens']) ? $data['scScreens'] : [];
        $scPictures = !empty($data['scPictures']) ? $data['scPictures'] : [];
        $mrScreens = !empty($data['mrScreens']) ? $data['mrScreens'] : [];
        $mrPictures = !empty($data['mrPictures']) ? $data['mrPictures'] : [];
        $lsScreens = !empty($data['lsScreens']) ? $data['lsScreens'] : [];
        $lsPictures = !empty($data['lsPictures']) ? $data['lsPictures'] : [];

        foreach ($scScreens as $key => $value) {
            $value['big_screen_url'] = $value['big_screen_type'] == 1 ? $value['big_screen_url'] : $value['big_video_url'];
            // unset($value['big_iamge_url']);
            unset($value['big_video_url']);
            $value['update_time'] = time();
            $model->where('id', $value['id'])->update($value);
        }

        foreach ($mrScreens as $key => $value) {
            $value['big_screen_url'] = $value['big_screen_type'] == 1 ? $value['big_screen_url'] : $value['big_video_url'];
            // unset($value['big_iamge_url']);
            unset($value['big_video_url']);
            $value['update_time'] = time();
            $model->where('id', $value['id'])->update($value);
        }

        foreach ($mrPictures as $key => $value) {
            $value['update_time'] = time();
            $model->where('id', $value['id'])->update($value);
        }

        foreach ($lsScreens as $key => $value) {
            $value['big_screen_url'] = $value['big_screen_type'] == 1 ? $value['big_screen_url'] : $value['big_video_url'];
            // unset($value['big_iamge_url']);
            unset($value['big_video_url']);
            $value['update_time'] = time();
            $model->where('id', $value['id'])->update($value);
        }
        $this->success(lang("SAVE_SUCCESS"));
    }

}
